package com.yeyks.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.param.account.AppAddBusinessParam;
import com.yeyks.common.appcommon.param.account.AppQueryBusinessParam;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.em.ResponseStatus;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.common.utils.TextValidator;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.pojo.dto.AddSysUserDTO;
import com.yeyks.commonReference.pojo.param.AddBusinessParam;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 运营商管理， 卡商管理
 *
 * @author: dagny
 * @date: 2019/12/9 0009 11:11
 */
@Api(tags = "v1.8（运营商管理，卡商管理）")
@RestController
@RequestMapping("/admin/business")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppBusinessController extends BaseController {

    private final AccountInfoService accountInfoService;

    @ApiOperation("v1.8分页查询运营商,卡商管理")
    @PostMapping("/list")
    public CommonResult<CommonPage<AppAccountDTO>> queryPage(@Valid AppQueryBusinessParam param) {

        AppAccountDTO appAccountDTO = this.validToken(null, null, null);

        return accountInfoService.queryPageAppBusiness(param,
                MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("v1.8新增运营商")
    @PostMapping("/add")
    public CommonResult addBusiness(@Valid AppAddBusinessParam param) {

        AppAccountDTO appAccountDTO = this.validToken(param.getToken(), param.getVersion(), param.getPlatform());

        if (appAccountDTO.getType() != AccountType.Administrator) {
            throw new OftenFiledException("暂无权限访问");
        }
        //验证参数
        validAddParam(param);
        AddBusinessParam addBusinessParam = MyBeanUtils.convert(param, AddBusinessParam.class);
        //插入运营商20
        addBusinessParam.setType(AccountType.MinOperateMerchant);
        //执行添加
        try {
            accountInfoService.saveBusiness(MyBeanUtils.convert(appAccountDTO, AccountInfo.class),
                    addBusinessParam);
        }catch (ServiceException e){
            throw new OftenFiledException(e.getMessage());
        }
        return CommonResult.success("操作成功");
    }

    /**
     * 验证添加参数信息
     *
     * @param param 输入参数信息
     */
    private void validAddParam(AppAddBusinessParam param) {

        if (null != param) {
            if (StringUtils.isNotEmpty(param.getPhone()) && !TextValidator.MOBILE.boolCheck(param.getPhone())) {
                throw new OftenFiledException("电话号码格式不正确");
            } else if (StringUtils.isNotEmpty(param.getUsername()) &&
                    param.getUsername().length() > 32) {
                // 4.输入的登陆用户名不对
                throw new OftenFiledException("登陆用户名超过32位");
            }
            if(StringUtils.isEmpty(param.getArea())){
                throw new OftenFiledException("请选择区域");
            }
            if(!param.getPassword().equals(param.getConfirmPwd())){
                throw new OftenFiledException("两次密码不一致！");
            }
            List<AccountInfo> existAccount = accountInfoService.list(new QueryWrapper<AccountInfo>()
                    .eq(AccountInfo.USERNAME, param.getUsername()));
            if (existAccount != null && existAccount.size() > 0) {
                throw new OftenFiledException("账号:" + param.getUsername() + "已存在");
            }
        }
    }


}
